#include "stm32f10x.h"
#include "key.h"
#include "adc.h"
#include "i2c.h"
#include "pwm.h"
#include "lcd.h"
#include "stdio.h"

u32 TimingDelay = 0;
u16 offset = 0;
u8 fre = 1;
extern u8 lcd_state;

//Main Body
int main(void)
{
	u16 phase = 0;
	float adc_val = 0;
	char text[50];
	SysTick_Config(SystemCoreClock/1000);
	STM3210B_LCD_Init();
	LCD_SetTextColor(White);
	LCD_SetBackColor(Blue2);
	LCD_Clear(Blue2);
	key_init();
	i2c_init();
	pwm_init();
	adc_init();
	offset = (get_adc()/3.3)*(10000/fre);
	
	while(1)
	{
		adc_val = get_adc();
		phase = (adc_val/3.3)*360;
		state_switch();
		switch(lcd_state){
			case output_pause:{
				sprintf(text,"ADC: %.2lfV     ",adc_val);
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"Output: OFF");
				LCD_DisplayStringLine(Line3,text);
				sprintf(text,"Phase Difference:%.2d",phase);
				LCD_DisplayStringLine(Line4,text);
				sprintf(text,"Frequency: %dKHz",fre);
				LCD_DisplayStringLine(Line5,text);
			}break;
			case output_running:{
				sprintf(text,"ADC: %.2lfV     ",adc_val);
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"Output: ON  ");
				LCD_DisplayStringLine(Line3,text);
				sprintf(text,"Phase Difference:%.2d",phase);
				LCD_DisplayStringLine(Line4,text);
				sprintf(text,"Frequency: %dKHz",fre);
				LCD_DisplayStringLine(Line5,text);
			}break;
			case setting:{
				LCD_ClearLine(Line3);
				LCD_ClearLine(Line5);
				sprintf(text,"      Setting       ");
				LCD_DisplayStringLine(Line2,text);
				sprintf(text,"Frequency: %dKHz       ",fre);
				LCD_DisplayStringLine(Line4,text);
				
			}break;
		}
	}
}

//
